package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xcity.db.entity.Player;
import com.xcity.game.common.ItemResponse;
import com.xcity.game.task.Task;
import com.xcity.game.task.TaskCondition;
import com.xcity.game.task.TaskDetector;
import com.xcity.game.task.TaskManager;
import com.xcity.game.task.TaskService;
import com.xcity.game.task.TaskTemplate;
import com.xcity.pomelo.response.ActivityNewPlayerInfoResponse.ItemSimpleInfo;

import naga.x.game.time.TimeUpdater;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class DayTaskResponse extends Response {
	
	private int finished;
	private int total;
	private int state;//(0未达到条件 1达到条件未领取  2已领取)
	private int stone;//奖励钻石
	private List<TaskInfo> list;
	
	public static class TaskInfo{
		public int id;
		public String name;
		public String desc;
		public int exp;
		public int gold;
		public int stone;
		public int honor;
		public ItemResponse[] items;
		public int state;
		public TaskCondition cond;
		public int doing;
		public int total;
		public TaskInfo(Task task, int progress){
			TaskTemplate template = task.getTemplate();
			this.id = task.getId();
			this.name = template.getName();
			this.desc = template.getDesc();
			this.exp = template.getExp();
			this.gold = template.getGold();
			this.stone = template.getStone();
			this.honor = template.getHonor();
			this.items = template.getItemObjs();
			this.state = task.getState().ordinal();
			this.cond = TaskCondition.create(template);
			this.total = this.cond.toClientCond();
			this.doing = progress;
		}
	}
	
	public DayTaskResponse(int serial, TaskManager vm, int playerLvl) {
		super(serial);

		total = 0;
		finished = 0;
		list = new ArrayList<TaskInfo>();
		List<Task> taskList = vm.getTasks();
		for(int i = 0; i < taskList.size(); i++){
			Task task = taskList.get(i);
			if(task.getTemplate().getType() == Task.TYPE_DAILY){
				if(playerLvl >= task.getTemplate().getNeedLvl()){
					JSONObject archive = task.getArchive();
					int progress = 0;
					if(task.getTemplate().getConditionId() == TaskDetector.TAP_LIKE){
						JSONArray data = task.getArchive().getJSONArray(String.valueOf(task.getTemplate().getConditionId()));
						if(data != null){
							progress = data.getIntValue(1);
						}
//					}else if(task.getTemplate().getConditionId() == TaskDetector.DAILY_SIGN){
//						progress = 0;
					}else{
						progress = archive.getIntValue(String.valueOf(task.getTemplate().getConditionId()));
					}
					TaskInfo taskInfo = new TaskInfo(task, progress);
					list.add(taskInfo);
					total++;
					if(task.getState() == Task.State.FINISHED || task.getState() == Task.State.REWARDED){
						//已完成
						finished++;
					}
				}
			}
		}
		stone = TaskService.DAILY_TASK_ALL_REWARD;
		if(finished < total){
			state = 0;
		}else{
			int today = TimeUpdater.getInstance().today();
			if(today == vm.getGetDay()){
				state = 2;
			}else{
				state = 1;
			}
		}
	}

	public List<TaskInfo> getList() {
		return list;
	}

	public void setList(List<TaskInfo> list) {
		this.list = list;
	}

	public int getFinished() {
		return finished;
	}

	public void setFinished(int finished) {
		this.finished = finished;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getStone() {
		return stone;
	}

	public void setStone(int stone) {
		this.stone = stone;
	}
	
}
